package com.example.njut;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.SystemClock;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Date;

public class TimeActivity extends AppCompatActivity {
    private Chronometer cc;
    private Button timeStart;
    private Button timePause;
    private Button timeReset;
    Vibrator vv;
    long tt; //计时器休眠时间
    String getTime;
    int on = 0;
    private TextView time1;
    private TextView time2;
    private TextView time3;
    private TextView time4;
    private TextView time5;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time);

        cc = (Chronometer) findViewById(R.id.time);
        timeStart = (Button) findViewById(R.id.time_start);
        timePause = (Button) findViewById(R.id.time_pause);
        timeReset = (Button) findViewById(R.id.time_reset);
        time1 = (TextView) findViewById(R.id.time1);
        time2 = (TextView) findViewById(R.id.time2);
        time3 = (TextView) findViewById(R.id.time3);
        time4 = (TextView) findViewById(R.id.time4);
        time5 = (TextView) findViewById(R.id.time5);
        vv = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);

        cc.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer chronometer) {
                String time = cc.getText().toString();
                int newTime = Integer.parseInt(time.substring(0,time.indexOf(":")))*60
                        +Integer.parseInt(time.substring(time.length()-2));
                if(newTime%10==0&&newTime!=0){
                    //震动5秒
                    //vv.vibrate(5000);
                    /*long []pattern = {5000,1000,5000,1000};
                    vv.vibrate(pattern,10);
                    vv.cancel();*/
                    vv.vibrate(new long[]{0,500},-1);
                }
            }
        });
        timeStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cc.setBase(SystemClock.elapsedRealtime()-tt);
                cc.start();
                timeStart.setEnabled(false);
                timeStart.setText("继  续");
                timePause.setEnabled(true);
                timeReset.setEnabled(true);
            }
        });

        timePause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tt = SystemClock.elapsedRealtime()-cc.getBase();
                cc.stop();
                timeStart.setEnabled(true);
                timePause.setEnabled(false);
                timeReset.setEnabled(true);
                getTime = cc.getText().toString();
                on++;
                if(on>5){
                    Toast.makeText(TimeActivity.this,"最多记5次",Toast.LENGTH_SHORT).show();
                    on=on-5;
                }
                if(on == 1){
                    time1.setText(getTime);
                }else if(on == 2){
                    time2.setText(getTime);
                }else if(on == 3){
                    time3.setText(getTime);
                }else if(on == 4){
                    time4.setText(getTime);
                }else if(on == 5){
                    time5.setText(getTime);
                }


            }
        });
        timeReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cc.setBase(SystemClock.elapsedRealtime());
                tt = 0;
                cc.stop();
                timeStart.setEnabled(true);
                timeStart.setText("开  始");
                timePause.setEnabled(false);
                timeReset.setEnabled(false);
                time1.setText("未计时");
                time2.setText("未计时");
                time3.setText("未计时");
                time4.setText("未计时");
                time5.setText("未计时");
            }
        });
        //暂停时记录时间
        //最多纪录5次，重置时记录被清空
        //第6次时，提示只能记5次
        //第6次变成第一次
    }
}
